package com.zhaojiyuan.boot.base.mybatis.create;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.io.File;
import java.util.Collections;

public class MybatisCreateMain {

  private static final String MAPPER_PATH = "mapper";
  // db 配置
  private static final String DB_URL = "jdbc:mysql://192.168.0.193:3306/test?characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true";
  private static final String DB_USERNAME = "root";
  private static final String DB_PASSWORD = "zhaojiyuanZZ123.";

  // 全局配置
  private static final String AUTHOR = "zhaojiyuan";
  private static final String PARENT = "com.zhaojiyuan.boot.base.rest.a.db.test";

  private static final String BASE_OUTPUT_DIR = new File("").getAbsolutePath();
  private static final String SRC_OUTPUT_DIR = BASE_OUTPUT_DIR + File.separator + "src" + File.separator + "main" + File.separator + "java";
  private static final String MAPPER_OUTPUT_DIR =
      BASE_OUTPUT_DIR + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator + "mapper";

  private static final String MODULE_NAME = "";

  private static final String[] TABLE_NAME_ARR = {"t_a_db_test_test"};

  public static void main(String[] args) {

    FastAutoGenerator.create(DB_URL, DB_USERNAME, DB_PASSWORD)
        .globalConfig(builder -> builder.author(AUTHOR).outputDir(SRC_OUTPUT_DIR).fileOverride().disableOpenDir())
        .packageConfig(builder -> overridePackage(builder))
        .strategyConfig(builder -> builder.addInclude(TABLE_NAME_ARR).entityBuilder().enableLombok().mapperBuilder().enableMapperAnnotation())
        .templateEngine(new FreemarkerTemplateEngine())
        .execute();
  }

  public static void overridePackage(PackageConfig.Builder builder) {
    builder.parent(PARENT)
        .entity("model")
        .mapper("mapper")
        .xml(MAPPER_OUTPUT_DIR)
        .service("service")
        .serviceImpl("service.impl")
        .controller("controller")
        .pathInfo(Collections.singletonMap(OutputFile.xml, MAPPER_OUTPUT_DIR));
  }

}
